package com.itboye.lottery.fragment.home.shuangseqiu


import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.itboye.bylibrary.util.TimeUtils
import com.itboye.lottery.R
import com.itboye.lottery.activity.WebActivity
import com.itboye.lottery.activity.daletou.DaLeTouMainActivity
import com.itboye.lottery.adapter.ShuangSeQiuCodeAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseFragment
import com.itboye.lottery.bean.CodeBean
import com.itboye.lottery.bean.ColorBallPrizeHisBean
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.CodeUtil.initCodeData
import com.itboye.lottery.utils.Combination
import com.itboye.lottery.utils.ConstUrl
import kotlinx.android.synthetic.main.fragment_shuangseqiu.*


/**
 * A simple [Fragment] subclass.
 */
class DaleTouFragment : BaseFragment(), ResponseCallback {
    private var colorBallPrizeHisBean: ColorBallPrizeHisBean = ColorBallPrizeHisBean()
    var tv_colorball_play_method: TextView? = null
    override fun success(o: Any, requestSuccessType: String) {
        if (requestSuccessType == userPresenter.daLeTouPrizeHistory_SUCCESS) {
            colorBallPrizeHisBean = o as ColorBallPrizeHisBean
            try {
                setHistoryData()
            } catch (e: Exception) {

            }
        }
    }

    private fun setHistoryData() {
        tv_shuangseqiu_message_2.removeAllViews()
        tv_shuangseqiu_message_1!!.text = "第${colorBallPrizeHisBean!!.newest.phaseno}期 ${TimeUtils.getFormatTime(colorBallPrizeHisBean!!.newest.localendtime.toLong() * 1000, "yyyy-MM-dd HH:mm:ss")}截止投注"
        if (colorBallPrizeHisBean!!.his!!.size > 0) {
            var textView = TextView(activity)
            textView.text = "第" + colorBallPrizeHisBean!!.his!![0].number + "期 "
            tv_shuangseqiu_message_2.addView(textView)
            colorBallPrizeHisBean!!.his!![0].red_num.forEachIndexed { index, s ->
                var textView = TextView(activity)
                textView.setBackgroundResource(R.drawable.solid_circle_border_red_small)
                textView.text = "$s"
                textView.gravity = Gravity.CENTER
                textView.setTextColor(resources.getColor(R.color.white))
                val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.setMargins(10, 10, 0, 10)
                tv_shuangseqiu_message_2.addView(textView, layoutParams)
            }
            colorBallPrizeHisBean!!.his!![0].blue_num.forEachIndexed { index, s ->
                var textView = TextView(activity)
                textView.setBackgroundResource(R.drawable.solid_circle_border_blue_small)
                textView.text = "$s"
                textView.gravity = Gravity.CENTER
                textView.setTextColor(resources.getColor(R.color.white))
                val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.setMargins(10, 10, 0, 10)
                tv_shuangseqiu_message_2.addView(textView, layoutParams)
            }
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        if (requestErrorType == userPresenter.daLeTouPrizeHistory_FAIL) {
            ByAlert.alert(msg)
        }
    }

    companion object {
        fun newInstance(type: Int): DaleTouFragment {
            val fragment = DaleTouFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            fragment.arguments = bundle
            return fragment
        }
    }

    var type = 0
    var userPresenter: UserPresenter = UserPresenter(this)
    override fun getLayoutId(): Int {
        return R.layout.fragment_shuangseqiu
    }

    override fun initData() {
        initHis()
        initCodeType()
        initRecyclerViewData()
    }

    private fun initHis() {
        userPresenter.daLeTouPrizeHistory()
    }

    private fun initCodeType() {

        type = arguments!!.getInt("type")//0：普通投注  1：胆拖投注
        when (type) {
            0 -> {
                //普通投注
                tv_red.text = "红球(前区)"
                tv_red_count.text = "最少选择5个"
                tv_blue.text = "蓝球(后区)"
                tv_blue_count.text = "最少选择2个"
                ll_blue_tuo.visibility = View.GONE
                ll_tuoma.visibility = View.GONE
            }
            1 -> {
                //胆拖投注
                tv_red.text = "胆码:红球(前区)"
                tv_red_count.text = "最少选择1个最多选择4个"
                tv_red_tuoma.text = "托码:红球(前区)"
                tv_red_tuoma_count.text = "最少选择2个"

                tv_blue_tuo.text = "托码:蓝球(后区)"
                tv_blue_tuo_count.text = "最少选择2个"
                tv_blue.text = "胆码:蓝球(后区)"
                tv_blue_count.text = "最多选择1个"
                ll_blue_tuo.visibility = View.VISIBLE
                ll_tuoma.visibility = View.VISIBLE
            }
            else -> {
            }
        }
    }

    var arrayRedCode: ArrayList<CodeBean>? = null
    var arrayRedTuoCode: ArrayList<CodeBean>? = null
    var arrayBlueCode: ArrayList<CodeBean>? = null
    var arrayBlueTuoCode: ArrayList<CodeBean>? = null

    var redAdapter: ShuangSeQiuCodeAdapter? = null
    var blueAdapter: ShuangSeQiuCodeAdapter? = null
    var redTuoMaAdapter: ShuangSeQiuCodeAdapter? = null
    var blueTuoMaAdapter: ShuangSeQiuCodeAdapter? = null

    private fun initRecyclerViewData() {
        initCode()//
        //红色胆码
        redAdapter = ShuangSeQiuCodeAdapter(this, true, arrayRedCode)
        rv_red_ball.layoutManager = GridLayoutManager(activity, 7)
        rv_red_ball.adapter = redAdapter

        //蓝色胆码
        rv_blue_ball.layoutManager = GridLayoutManager(activity, 7)
        blueAdapter = ShuangSeQiuCodeAdapter(this, false, arrayBlueCode)
        rv_blue_ball.adapter = blueAdapter

        if (type == 1) {
            //只有胆拖投注才显示  红色托码
            redTuoMaAdapter = ShuangSeQiuCodeAdapter(this, true, arrayRedTuoCode)
            rv_red_ball_tuoma.layoutManager = GridLayoutManager(activity, 7)
            rv_red_ball_tuoma.adapter = redTuoMaAdapter

            //蓝色托码
            blueTuoMaAdapter = ShuangSeQiuCodeAdapter(this, false, arrayBlueTuoCode)
            rv_blue_tuo_ball.layoutManager = GridLayoutManager(activity, 7)
            rv_blue_tuo_ball.adapter = blueTuoMaAdapter
        }
        redAdapter!!.setOnItemClickListener({ view, position ->

            if (!arrayRedCode!![position].isSelect) {
                if (type == 0) {

                }
                //胆拖红球1-4
                else if (type == 1) {
                    //红胆不能与红拖数字相等
                    arrayRedTuoCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect and (codeBean.codeValue == arrayRedCode!![position].codeValue)) {
                            ByAlert.alert("胆码与拖码不能相等")
                            return@setOnItemClickListener
                        }
                    }
                    var selectCount = 0
                    arrayRedCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect) {
                            selectCount++
                        }
                    }
                    if (selectCount >= 4) {
                        ByAlert.alert("红色球个数最多选择4个")
                        return@setOnItemClickListener
                    }
                }
            }

            arrayRedCode!![position].isSelect = !arrayRedCode!![position].isSelect
            redAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
        redTuoMaAdapter?.setOnItemClickListener({ view, position ->
            //红球托码个数最少一个
            //红胆不能与红拖数字相等
            if (!arrayRedTuoCode!![position].isSelect) {
                arrayRedCode!!.forEachIndexed { index, codeBean ->
                    if (codeBean.isSelect and (codeBean.codeValue == arrayRedTuoCode!![position].codeValue)) {
                        ByAlert.alert("胆码与拖码不能相等")
                        return@setOnItemClickListener
                    }
                }
            }

            arrayRedTuoCode!![position].isSelect = !arrayRedTuoCode!![position].isSelect
            redTuoMaAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
        blueAdapter!!.setOnItemClickListener({ view, position ->
            //胆拖蓝胆球最多一个
            if (type == 1) {
                if (!arrayBlueCode!![position].isSelect) {
                    arrayBlueTuoCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect and (codeBean.codeValue == arrayBlueCode!![position].codeValue)) {
                            ByAlert.alert("胆码与拖码不能相等")
                            return@setOnItemClickListener
                        }
                    }

                    var selectCount = 0
                    arrayBlueCode!!.forEachIndexed { index, codeBean ->
                        if (codeBean.isSelect) {
                            selectCount++
                        }
                    }
                    if (selectCount >= 1) {
                        ByAlert.alert("蓝色胆球个数最多选择1个")
                        return@setOnItemClickListener
                    }
                }
            }
            arrayBlueCode!![position].isSelect = !arrayBlueCode!![position].isSelect
            blueAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
        blueTuoMaAdapter?.setOnItemClickListener({ view, position ->
            if (!arrayBlueTuoCode!![position].isSelect) {
                arrayBlueCode!!.forEachIndexed { index, codeBean ->
                    if (codeBean.isSelect and (codeBean.codeValue == arrayBlueTuoCode!![position].codeValue)) {
                        ByAlert.alert("胆码与拖码不能相等")
                        return@setOnItemClickListener
                    }
                }
            }
            arrayBlueTuoCode!![position].isSelect = !arrayBlueTuoCode!![position].isSelect
            blueTuoMaAdapter!!.notifyDataSetChanged()
            caculateZhu()
        })
//        rv_football.layoutManager=LinearLayoutManager(activity)
//        rv_football.adapter=FootballJinCaiAdapter()
    }

    fun caculateZhu() {
        var arrayRed = redAdapter!!.selectCodeArray
        var arrayRedTuo = redTuoMaAdapter?.selectCodeArray
        var arrayBlue = blueAdapter!!.selectCodeArray
        var arrayBlueTuo = blueTuoMaAdapter?.selectCodeArray
        //计算注数
        var num = Combination.findsort(arrayRed, 5)
        var num2 = Combination.findsort(arrayBlue, 1)
        var num3 = 1
        var num4 = 1
        when ((activity as DaLeTouMainActivity).selectionPosition) {
            0 -> {
                num2 = Combination.findsort(arrayBlue, 2)
                //普通
                (activity as DaLeTouMainActivity).setCalculateZhuShu(num * num2)
            }
            1 -> {
                num = Combination.findsort(arrayRed, 4)
                num3 = Combination.findsort(arrayRedTuo, 5 - arrayRed.size)
                num4 = Combination.findsort(arrayBlueTuo, 2 - arrayBlue.size)
                //胆拖
                (activity as DaLeTouMainActivity).setCalculateZhuShu(num * num2 * num3 * num4)
            }
            else -> {
            }
        }
    }

    private fun initCode() {
        arrayRedCode = initCodeData(33, 0)//红色
        arrayRedTuoCode = initCodeData(33, 1)//红色脱码
        arrayBlueCode = initCodeData(12, 2)//蓝色球
        arrayBlueTuoCode = initCodeData(12, 3)//蓝色托码
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_colorball_play_method -> {
                startActivity(Intent(activity, WebActivity::class.java).putExtra("title", "玩法说明").putExtra("url", ConstUrl.play_method_url + "?type=" + ConstUrl.TYPE_DALETOU))
            }
            else -> {
            }
        }
    }

}
